package com._520it.smis.web.servlert;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com._520it.smis.dao.IStudentDao;
import com._520it.smis.dao.impl.StudentDaoImpl;
import com._520it.smis.domain.Student;

@WebServlet("/student/save")
public class SaveStudentServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private IStudentDao dao;

	@Override
	public void init() throws ServletException {
		dao = new StudentDaoImpl();
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		// 1. 接收请求参数
		String sId = req.getParameter("id");
		String name = req.getParameter("name");
		String sAge = req.getParameter("age");
		// 2. 调用业务方法
		Student stu = new Student();
		stu.setName(name);
		stu.setAge(Integer.valueOf(sAge));
		if (sId != null && !"".equals(sId)) {
			stu.setId(Long.valueOf(sId));
			dao.update(Long.valueOf(sId), stu);
		} else {
			dao.save(stu);
		}
		// 3.跳转界面
		resp.sendRedirect("/student/list");
		System.out.println("edit come");
	}

}
